﻿using Crawler.Core.CrawlerHandler.Collector;
using Crawler.Core.EventBus;
using Crawler.IRepositories;
using Crawler.IRepositories.SystemDefault;
using Crawler.Model.SystemDefault;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace Crawler.Core.CrawlerHandler.UrlChecker
{
    public class UrlCheckerHandler : IEventHandler<UrlCheckerEventEntity>
    {
        private readonly IRepository<CollectInfoConfigModel> collectInfoConfigRepository;
        private readonly ICollectorUrlRepository collectorUrlRepository;
        private readonly IEventBus eventBus;

        public UrlCheckerHandler(IRepository<CollectInfoConfigModel> collectInfoConfigRepository, ICollectorUrlRepository collectorUrlRepository, IEventBus eventBus)
        {
            this.collectInfoConfigRepository = collectInfoConfigRepository;
            this.collectorUrlRepository = collectorUrlRepository;
            this.eventBus = eventBus;
        }

        public void Trigger(UrlCheckerEventEntity entity)
        {
            if (entity.collectorUrlModel == null) return;
            var urlModel = entity.collectorUrlModel;
            //获取配置信息
            var configModel = collectInfoConfigRepository.Find(urlModel.CollectorConfigID);
            //配置设置停止则终止
            if (configModel == null || configModel.WorkStatus != WorkStatus.Running)
            {
                //Console.WriteLine("#####UrlChecker Stop");
                return;
            }
            Console.WriteLine("#####UrlChecker Check:" + urlModel.Url);
            //如果不是重新采集状态，则要进行校验，是重新采集的话，将不做校验
            if (urlModel.CollectStatus != CollectStatus.ReCollect)
            {
                if (urlModel.AddTime < configModel.LatestCollectTime) return;

                //已经采集过了的话(url 存在，且 url 的添加日期在配置的最新采集时间之后)，跳过
                var existUrlModel = collectorUrlRepository.GetExistUrlInfo(configModel.ID, urlModel.Url);
                //这里一定要用 >= 因为时间的精度才到 s 所以会导致已开启就有一条插进来，时间就是相等的
                if (existUrlModel != null && existUrlModel.AddTime >= configModel.LatestCollectTime) return;
            }
            //如果设置了过滤 host，不在范围内的直接忽略
            if(!string.IsNullOrEmpty(configModel.FilterHost))
            {
                var contains = false;
                foreach (var item in configModel.FilterHost.Split(","))
                {
                    if (urlModel.Url.Contains(item))
                    {
                        contains = true;
                        break;
                    }
                }
                if (!contains) return;
            }


            //深度 +1
            urlModel.Depth++;
            //达到指定深度则停止
            if (configModel.Depth < urlModel.Depth) return;
            //default 未 未处理状态
            urlModel.CollectStatus = CollectStatus.Default;

            //TODO 保存到数据库
            collectorUrlRepository.Add(urlModel);

            //TODO 发布到事件总线,下一个是采集器
            eventBus.Publish(new CollectorEventEntity { collectorUrlModel = urlModel });
        }
    }
}
